package com.pactera.madp.cp.controller.oms.greeting;

/**
 * @description:好友欢迎语
 * @author: Huayu
 * @time: 2021/2/1 14:54
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.greeting.GreetingDto;
import com.pactera.madp.cp.api.dto.greeting.GreetingPageDTO;
import com.pactera.madp.cp.api.entity.medium.MediumEnyity;
import com.pactera.madp.cp.api.vo.greeting.GreetingPageVO;
import com.pactera.madp.cp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@AllArgsConstructor
@RequestMapping("/greeting")
@Api(value = "greeting", tags = "好友欢迎语")
public class GreetingController {

    private IGreetingService greetingServiceImpl;
    private IMediumService mediumServiceImpl;

    /**
     * @description:欢迎语列表
     * @author: Huayu
     * @time: 2021/2/2 15:19
     */
    @GetMapping("/page")
    @ApiOperation(value = "欢迎语列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="permission",value="权限:ALL-全部,DEPARTMENT-部门,EMPLOYEE-员工")
    })
    public R<Page<List<GreetingPageVO>>> greetingIndex(Page page, GreetingPageDTO dto) {
        return R.ok(greetingServiceImpl.handle(page, dto));
    }

    @SysLog("创建欢迎语")
    @PostMapping
    @ApiOperation(value = "创建欢迎语")
    public R<Boolean> greetingStore(@Validated()  @RequestBody GreetingDto greeting) {
        return greetingServiceImpl.saveGreeting(greeting) ?R.ok(Boolean.TRUE,"新增成功"):R.failed(Boolean.FALSE,"新增失败");
    }

    @SysLog("编辑欢迎语")
    @PutMapping
    @ApiOperation(value = "编辑欢迎语")
    public R<Boolean> greetingUpdate(@Validated()  @RequestBody GreetingDto greeting) {
        return greetingServiceImpl.updateGreeting(greeting) ?R.ok(Boolean.TRUE,"修改成功"):R.failed(Boolean.FALSE,"修改失败");
    }

    @SysLog("删除欢迎语")
    @DeleteMapping("/{greetingId}")
    @ApiOperation(value = "删除欢迎语")
    @ApiImplicitParams({
            @ApiImplicitParam(name="greetingId",value="欢迎语id",dataType = "Integer")
    })
    public R<Boolean> greetingDelete(@PathVariable Integer greetingId) {
        return greetingServiceImpl.deleteGreeting(greetingId) ?R.ok(Boolean.TRUE,"删除成功"):R.failed(Boolean.FALSE,"删除失败");
    }

    /**
     * @description:欢迎语 - 详情
     * @author: Huayu
     * @time: 2021/2/3 16:31
     */
    @GetMapping("/{greetingId}")
    @ApiOperation(value = "欢迎语详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="greetingId",value="欢迎语id",dataType = "Integer")
    })
    public R<GreetingPageVO> greetingShow(@PathVariable Integer greetingId){
        return R.ok(greetingServiceImpl.getGreetingById(greetingId));
    }

    private JSONObject getMediumContent(Integer mediumId) {
        MediumEnyity mediumEnyity = mediumServiceImpl.getMediumById(mediumId);
        if (mediumEnyity == null) {
            return new JSONObject();
        }
        String str = mediumServiceImpl.addFullPath(mediumEnyity.getContent(), mediumEnyity.getType());
        JSONObject json = (JSONObject) JSON.parse(str);
        return json;
    }


    @GetMapping("/text")
    public R getText(String userId){
        return R.ok(greetingServiceImpl.getGreeting(userId));
    }

}
